package configuration

import (
	"encoding/json"
	"fmt"
	"kop/modules/configuration/client"
	"kop/util/log"
	"kop/util/rand"
)

func init() {

	if err := json.Unmarshal(client.Load("InvestTemplate"), &PortConfigs); err != nil {
		panic(err)
	}

	log.Info("Load InvestTemplate.json success.")

	for _, v := range PortConfigs {
		RandConfigs = append(RandConfigs, v)
	}
}

type PortConf struct {
	PortID          int32   `json:"ID"` // 港口ID
	Weight          int32   // 权重
	GoldRate        float64 // 特产金币比例
	ItemID          int32   // 特产物品ID
	ItemNumber      int32   // 特产物品数量
	LevelUpGoldRate float64 // 金币回报比例
}

func (p PortConf) GetID() int32 {
	return p.PortID
}

func (p PortConf) GetWeight() int32 {
	return p.Weight
}

var (
	PortConfigs []PortConf
	RandConfigs []rand.Value
)

func GetByPortID(portID int32) PortConf {

	for _, v := range PortConfigs {
		if v.PortID == portID {
			return v
		}
	}

	panic(fmt.Errorf("PortID not found [%d]", portID))
}

func Rand3() []PortConf {

	var res []PortConf

	for _, v := range rand.WithWeight(RandConfigs, 3) {
		res = append(res, v.(PortConf))
	}

	return res
}
